package cn.broadin.app.hotel.util;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.SparseIntArray;

/**
 * 播放音效工具类
 * 
 * @author   long
 * @version  1.0.0
 * @date 2015年9月15日下午4:29:56
 */
public class XSound {

	private Context context;
	private AudioManager mgr;

	public XSound(Context context) {
		this.context = context;
		initSounds();
	}

	// 音效的音量
	private int streamVolume;
	private int audioMaxVolumn;
	// 定义SoundPool 对象
	private SoundPool soundPool;

	// 定义HASH表
	private SparseIntArray soundPoolMap;

	public void initSounds() {
		// 初始化soundPool 对象,第一个参数是允许有多少个声音流同时播放,第2个参数是声音类型,第三个参数是声音的品质
		soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 100);

		// 初始化HASH表
		soundPoolMap = new SparseIntArray();

		// 获得声音设备和设备音量
		mgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		streamVolume = mgr.getStreamVolume(AudioManager.STREAM_SYSTEM);

		if (streamVolume == 0) {
			streamVolume = 1;
		}

		// 最大音量
		audioMaxVolumn = mgr.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);

	}

	public void loadSfx(int raw, int priority) {
		soundPoolMap.put(raw, soundPool.load(context, raw, priority));
	}

	public void play(int sound, int uLoop) {

		soundPool.play((Integer) soundPoolMap.get(sound), audioMaxVolumn
				/ streamVolume, audioMaxVolumn / streamVolume, 0, 0, 1f);
	}
	
	public void play(int sound){
		soundPool.play((Integer) soundPoolMap.get(sound), audioMaxVolumn
				/ streamVolume, audioMaxVolumn / streamVolume, 0, 0, 1f);
	}

	public void pause(int sound) {
		soundPool.pause((Integer) soundPoolMap.get(sound));
	}
	
	public void moveFocusLeft(){
		mgr.playSoundEffect(AudioManager.FX_KEY_CLICK);
	}
	
	
}
